<template>
  <div class="sub-content-box">
    <div class="chart-box" v-for="item in typeList" :key="item.dictCode">
      <Line class="chart" :loading="item.loading" :chartData="item.chartData" :title="item.itemName" :showNumber="1" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import Line from '@/components/Charts/Line/index.vue'
import { ref, onMounted, watch, computed } from 'vue'
import { getChartChildListData, GETchildDatAverageSum } from '@/api/common/chart'
import { getDictListByCode } from '@/api/sys/dict'

defineOptions({ name: 'MauSystem' })

const props = defineProps({
  searchInfo: {
    type: Object,
    default: () => ({}),
  },
  label: {
    type: String,
    default: '',
  },
})

const typeList = ref([])
const chartList = ref([])
const dataReady = ref(false)
let dictCode = ref('')

// 处理字典code
const formateDictCode = () => {
  let name = '';
  switch (props.label) {
    case '其他':
      name = 'other'
      break;
    case '化学品':
      name = 'chemical'
      break;
    default:
      name = props.label.toLowerCase()
      break;
  }
  dictCode.value = `energy_consumption_of_${name}_system`
}

const loading = computed(() => {
  return !dataReady.value
})

onMounted(async () => {
  formateDictCode()
  await getTypeList()
})

const handleSearch = (params) => {
  if (dataReady.value) {
    getChartData(params)
  } else {
    setTimeout(() => {
      handleSearch(params)
    }, 200)
  }
}

// 监测查询参数是否变化
watch(
  () => props.searchInfo,
  (newValue) => {
    if (Object.values(newValue).length > 0) {
      handleSearch(newValue)
    }
  },
  { immediate: true },
)

const getTypeList = async () => {
  const res = await getDictListByCode(dictCode.value)
  // const resS = await getDictListByCode('energy_consumption_of_mau_system_songfeng')
  // console.log(resS);

  dataReady.value = true
  typeList.value = res.map((item) => ({
    ...item,
    loading: true,
    chartData: {
      xaxisList: [],
      yaxisList: [],
    },
  }))
  // typeList.value.push({
  //   dictCode: "energy_consumption_of_mau_system_songfeng",
  //   itemName: '送风量',
  //   loading: true,
  //   chartData: {
  //     xaxisList: [],
  //     yaxisList: [],
  //   }
  // })
}

const getChartData = (searchInfo) => {
  typeList.value.forEach((item) => {

    item.loading = true
    getChartChildListData({
      ...searchInfo,
      dictCode: item.dictCode,
      itemName: item.itemName,
    })
      .then((res) => {
        item.chartData = res
      })
      .finally(() => {
        item.loading = false
      })
  })
  console.log(typeList.value);

  // GETchildDatAverageSum({ ...searchInfo, dictCode: 'energy_consumption_of_mau_system_songfeng', itemName: '送风量' }).then(res => {
  //   console.log(res);

  //   // typeList.value.push({ chartData: res })
  // })
}
</script>

<style scoped lang="scss">
.sub-content-box {
  display: flex;
  flex-wrap: wrap;
  height: 100%;

  .chart-box {
    flex-shrink: 0;
    width: 50%;
    height: 50%;

    /* height: 100%; */
    padding: 4px;
  }

}
</style>
